package cate.game.activity.specialtraining.po;

import cate.common.table.activity.specialtraining.point.SpecialTrainingPointRow;
import cate.common.table.activity.specialtraining.point.SpecialTrainingPointSheet;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.activity.specialtraining.SpecialTrainingActivity;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.Map;

@NoteClass(value = "特训活动-积分奖励")
public class SpecialTrainingActivityPointPO extends SubActivitySingle<SpecialTrainingActivity> {
    @NoteField(value = "积分")
    public long point;
    @NoteField(value = "积分奖励领取MAP")
    public Map<Integer, Boolean> pointRewardGotMap;

    @Override
    public void initialize(SpecialTrainingActivity parent) {
        super.initialize(parent);
        if (pointRewardGotMap == null) {
            pointRewardGotMap = new HashMap<>();
        }
    }

    public void excelCheck() {
        SpecialTrainingPointSheet pointSheet = parent.getPointSheet();
        if (pointSheet == null) {
            pointRewardGotMap.clear();
            return;
        }
        pointRewardGotMap.entrySet().removeIf(entry -> pointSheet.get(entry.getKey()) == null);
        for (SpecialTrainingPointRow configRow : pointSheet.getList()) {
            if (!pointRewardGotMap.containsKey(configRow.id)) {
                pointRewardGotMap.put(configRow.id, false);
            }
        }
    }

    public void beforeOver() {

    }

    public void clear() {
        pointRewardGotMap.clear();
    }

    /**
     * 获取积分奖励配置
     *
     * @param tid 配置ID
     */
    @JsonIgnore
    public SpecialTrainingPointRow getConfig(int tid) {
        SpecialTrainingPointSheet pointSheet = parent.getPointSheet();
        if (pointSheet == null) {
            return null;
        }
        return pointSheet.get(tid);
    }

    /**
     * 领取积分奖励
     *
     * @param tid 配置ID
     * @return 领取结果 无携带数据
     */
    @JsonIgnore
    public EcResult<Void> receivePointReward(int tid) {
        EcResult<Void> r = new EcResult<>();
        SpecialTrainingPointRow configRow = getConfig(tid);
        if (configRow == null) {
            return r.fail("找不到配置");
        }
        if (!pointRewardGotMap.containsKey(tid)) {
            return r.fail("找不到配置");
        }
        if (pointRewardGotMap.get(tid)) {
            return r.fail("奖励已领取");
        }
        if (configRow.needPoint > point) {
            return r.fail("积分不足");
        }
        MixRes rewardRes = new MixRes(configRow.rewardStr);
        if (rewardRes.isEmpty()) {
            return r.fail("奖励未配置");
        }
        pointRewardGotMap.put(tid, true);
        new MixResAdder().setRes(rewardRes)
                .setOperation(parent.getGDOperation())
                .setOperationSub(tid)
                .setSendMailWhenFull(true)
                .setDialogReward(true)
                .exe(role);
        return r.success();
    }
}
